﻿using mediaControl.Event;
using mediaControl.Utils;
using mediaControl.Vo;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace mediaControl.ViewModels
{
    public class AddBlankViewModel : BindableBase, IDialogAware
    {
        //事件聚合器
        private readonly IEventAggregator _eventAggregator;

        private ProgramVo program;

        private string name;

        public DelegateCommand ConfirmCommand { get; set; }
        public DelegateCommand CloseCommand { get; set; }

        public AddBlankViewModel(IEventAggregator eventAggregator)
        {
            _eventAggregator = eventAggregator;
            ConfirmCommand = new DelegateCommand(confirm);
            CloseCommand = new DelegateCommand(closeWindow);
        }

        private void closeWindow()
        {
            RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.No));
        }

        private void confirm()
        {
            //保存节目
            if (null != program)
            {
                string fileFullPath = program.FilePath + "\\" + program.Name + ".info"; // 指定要创建的文件路径及名称
                try
                {
                    if (!File.Exists(fileFullPath))
                    {
                        MessageBox.Show("文件不存在！");
                        return;
                    }
                    long timeStamp = DateTimeOffset.Now.ToUnixTimeMilliseconds();
                    ProgramItemVo programItemVo = new ProgramItemVo(0, Name, "", "2", "", "", "", "",timeStamp+"");
                    string content = WriteFileUtil.readFile(fileFullPath);
                    if (!String.IsNullOrEmpty(content))
                    {
                        ObservableCollection<ProgramItemVo> programItems = new ObservableCollection<ProgramItemVo>();
                        JObject programJson = JsonConvert.DeserializeObject<JObject>(content);
                        if (programJson.ContainsKey("programItems"))
                        {
                            programItems = JsonConvert.DeserializeObject<ObservableCollection<ProgramItemVo>>((String)programJson.GetValue("programItems"));
                        }
                        programItemVo.No = programItems.Count + 1;
                        programItems.Add(programItemVo);
                        if (programJson.ContainsKey("programItems"))
                        {
                            programJson["programItems"] = JsonConvert.SerializeObject(programItems);
                        }
                        else
                        {
                            programJson.Add("programItems", JsonConvert.SerializeObject(programItems));
                        }
                        WriteFileUtil.writeFile(fileFullPath, JsonConvert.SerializeObject(programJson), "2");
                        _eventAggregator.GetEvent<ProgramItemEvent>().Publish(programItemVo);
                    }
                    IDialogParameters parameters = new DialogParameters();
                    RequestClose?.Invoke(new Prism.Services.Dialogs.DialogResult(ButtonResult.OK, parameters));


                }
                catch (Exception ex)
                {

                }

            }
        }

        public string Title { get; set; }
        public string Name { get { return name; } set { this.name = value; RaisePropertyChanged(); } }

        public event Action<IDialogResult> RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            program = parameters.GetValue<ProgramVo>("program");
            Title = "添加空白节目";
        }
    }
}
